package com.lzs.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzs.PO.Page;
import com.lzs.PO.Role;
import com.lzs.PO.RuralCmsOffice;
import com.lzs.PO.User;
import com.lzs.Service.RoleService;
import com.lzs.Service.RuralCmsOfficeService;
import com.lzs.Service.UserService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/user")
public class UserController {
	@Autowired
	UserService userService;
	@Autowired
	RoleService roleService;
	@Autowired
	RuralCmsOfficeService ruralcmsofficeService;

	@RequestMapping(value = "/user_list")
	public String roleList(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, String keyword) throws Exception {

		Page<User> page = this.userService.findUserPagelist(keyword, pageNum, pageSize);

		model.addAttribute("page", page);

		return "/user/user_list";
	}

	@RequestMapping(value = "/user_add")
	public String userAddLoadRolesData(Model model) {

		List<Role> roles = this.roleService.findRoles();

		model.addAttribute("roles", roles);

		return "/user/user_add";
	}

	@ResponseBody
	@RequestMapping(value = "/user_loadRuralCmsOfficeData")
	public Msg loadRuralCmsOfficeData() {
		try {

			List<RuralCmsOffice> ruralcmsofficelist = this.ruralcmsofficeService.findOffices();

			return Msg.success().add("ruralcmsofficelist", ruralcmsofficelist);

		} catch (Exception e) {

			e.printStackTrace();

			return Msg.fail();
		}
	}

	@ResponseBody
	@RequestMapping(value = "/user_add_action")
	public Msg addUserAction(HttpServletRequest request, Integer userid, String username, String password,
			String office_id, Integer[] roleids) {
		try {
			userid = Integer.parseInt(request.getParameter("userid"));
			// 添加角色信息
			User user = new User();

			user.setUserid(userid);

			user.setPassword(password);

			user.setUsername(username);

			user.setOffice_id(office_id);
			
			this.userService.addUser(user);
			// 添加用户角色信息
			Map<String, Object> map = new HashMap<String, Object>();

			map.put("roleids", roleids);

			map.put("userid", userid);

			this.userService.addUserRole(map);
			
			return Msg.success();
			
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail();
		}
	}

	@ResponseBody
	@RequestMapping(value = "/user_check")
	public Msg checkUser(Integer userid) {

		Boolean b = this.userService.countUserById(userid);
		if (b) {
			return Msg.success();
		} else {
			return Msg.fail();
		}
	}

	@RequestMapping(value = "/user_update")
	public String userUpdate(Integer userid, HttpServletRequest request, Model model) {

		userid = Integer.parseInt(request.getParameter("userid"));
		System.out.println("用户id:"+userid);
		User user = this.userService.findUserRoleById(userid);
        System.out.println("用户:"+user);
		RuralCmsOffice ruralcmsoffice = this.ruralcmsofficeService.findRuralCmsOfficeById(user.getOffice_id());

		List<Role> roles = this.roleService.findRoles();

		model.addAttribute("roles", roles);

		model.addAttribute("user", user);

		model.addAttribute("ruralcmsoffice", ruralcmsoffice);

		return "/user/user_update";
	}

	@ResponseBody
	@RequestMapping(value = "/user_update_action")
	public Msg updateUserAction(Integer userid, String username, String password, String office_id, Integer[] roleids) {

		try {


			Map<String, Object> map = new HashMap<String, Object>();

			map.put("userid", userid);

			map.put("roleids", roleids);
			
			map.put("username", username);
			
			map.put("password", password);
			
			map.put("office_id", office_id);

			this.userService.updateUserRole(map);

			return Msg.success();

		} catch (Exception e) {

			e.printStackTrace();

			return Msg.fail();
		}
	}

	@ResponseBody
	@RequestMapping(value = "/user_delete")
	public Msg deleteUser(Integer userid) {
		try {
			this.userService.deleteUser(userid);
			Map<String,Object>map = new HashMap<String,Object>();
			map.put("userid", userid);
			this.userService.deleteUserRoleById(map);
			
			return Msg.success();
		} catch (Exception e) {
			e.printStackTrace();
			return Msg.fail();
		}

	}
}
